//
//  ViewController.m
//  绘图板
//
//  Created by hyrMac on 15/8/5.
//  Copyright (c) 2015年 hyrMac. All rights reserved.
//

#import "ViewController.h"
#import "DrawGround.h"
#import "MyButton.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

typedef void (^Type) (void);

@interface ViewController ()
{
    UIView *_headerView;
    
    UIView *_colorToolView;
    UIView *_lineWidthToolView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _drawGround = [[DrawGround alloc] initWithFrame:self.view.bounds];
    _drawGround.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_drawGround];
    
    [self _createHeaderView];
    
    [self _createColorToolView];
    [self _createLineWidthToolView];
    
    
}

// 颜色 线宽 橡皮 撤销 清屏
- (void)_createHeaderView {
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 60)];
    _headerView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_headerView];
    
    MyButton *btn1 = [[MyButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/5, 60)];
    btn1.title = @"颜色";
    Type block1 = ^{
        _colorToolView.hidden = NO;
        _lineWidthToolView.hidden = YES;
        NSLog(@"颜色");
    };
    [btn1 setBlock:block1];
    [_headerView addSubview:btn1];
    
    
    MyButton *btn2 = [[MyButton alloc] initWithFrame:CGRectMake(kWidth/5, 0, kWidth/5, 60)];
    btn2.title = @"线宽";
    Type block2 = ^{
        _colorToolView.hidden = YES;
        _lineWidthToolView.hidden = NO;
        NSLog(@"线宽");
        
    };
    [btn2 setBlock:block2];
    [_headerView addSubview:btn2];
    
    
    MyButton *btn3 = [[MyButton alloc] initWithFrame:CGRectMake(kWidth/5*2, 0, kWidth/5, 60)];
    btn3.title = @"橡皮";
    Type block3 = ^{
        
        _drawGround.paintColor = [UIColor whiteColor];
    };
    [btn3 setBlock:block3];
    [_headerView addSubview:btn3];
    
    
    MyButton *btn4 = [[MyButton alloc] initWithFrame:CGRectMake(kWidth/5*3, 0, kWidth/5, 60)];
    btn4.title = @"撤销";
    Type block4 = ^{
        
        [_drawGround.allPoints removeLastObject];
        [_drawGround.points removeAllObjects];
        [_drawGround setNeedsDisplay];
       
    };
    [btn4 setBlock:block4];
    [_headerView addSubview:btn4];
    
    MyButton *btn5 = [[MyButton alloc] initWithFrame:CGRectMake(kWidth/5*4, 0, kWidth/5, 60)];
    btn5.title = @"清屏";
    Type block5 = ^{
        
        [_drawGround.points removeAllObjects];
        [_drawGround.allPoints removeAllObjects];
        [_drawGround setNeedsDisplay];
    };
    [btn5 setBlock:block5];
    [_headerView addSubview:btn5];
    
}

// 颜色板
- (void)_createColorToolView {
    
    NSArray *colorArray = @[[UIColor grayColor],[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor purpleColor],[UIColor brownColor],[UIColor blackColor]];
    _colorToolView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, kWidth, 80)];
    _colorToolView.backgroundColor = [UIColor lightGrayColor];
    _colorToolView.hidden = YES;
    [self.view addSubview:_colorToolView];
    
    // 创建颜色块
    for (NSInteger i = 0; i < colorArray.count; i++) {
        MyButton *btn = [[MyButton alloc] initWithFrame:CGRectMake(kWidth/(colorArray.count)*i, 0, kWidth/(colorArray.count)-5, 70)];
        btn.backgroundColor = colorArray[i];
        Type block = ^{
            
            _drawGround.paintColor = btn.backgroundColor;
//            NSLog(@"%@",_drawGround.paintColor);
        };
        [btn setBlock:block];
        [_colorToolView addSubview:btn];
    }
    
}

// 线宽选择
- (void)_createLineWidthToolView {
    
    _lineWidthToolView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, kWidth, 80)];
    _lineWidthToolView.backgroundColor = [UIColor lightGrayColor];
    _lineWidthToolView.hidden = YES;
    [self.view addSubview:_lineWidthToolView];
    
    for (NSInteger i = 0; i < 7; i++) {
        MyButton *btn = [[MyButton alloc] initWithFrame:CGRectMake(kWidth/7*i, 0, kWidth/7, 70)];
        btn.backgroundColor = [UIColor clearColor];
        btn.title = [NSString stringWithFormat:@"%ld点",(i+1)*5];
        Type block = ^{
            
            _drawGround.lineWidth = (i*5)+5;
        };
        [btn setBlock:block];
        [_lineWidthToolView addSubview:btn];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
